package cn.stylefeng.guns.modular.flowable.service.impl;

import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.common.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.flowable.entity.ActDeModel;
import cn.stylefeng.guns.modular.flowable.mapper.ActDeModelMapper;
import cn.stylefeng.guns.modular.flowable.model.params.ActDeModelParam;
import cn.stylefeng.guns.modular.flowable.model.result.ActDeModelResult;
import cn.stylefeng.guns.modular.flowable.service.ActDeModelService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 工作流模板 服务实现类
 * </p>
 *
 * @author gblfy
 * @since 2019-12-16
 */
@Service
public class ActDeModelServiceImpl extends ServiceImpl<ActDeModelMapper, ActDeModel> implements ActDeModelService {

    @Override
    public void add(ActDeModelParam param){
        ActDeModel entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ActDeModelParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ActDeModelParam param){
        ActDeModel oldEntity = getOldEntity(param);
        ActDeModel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ActDeModelResult findBySpec(ActDeModelParam param){
        return null;
    }

    @Override
    public List<ActDeModelResult> findListBySpec(ActDeModelParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ActDeModelParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ActDeModelParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ActDeModel getOldEntity(ActDeModelParam param) {
        return this.getById(getKey(param));
    }

    private ActDeModel getEntity(ActDeModelParam param) {
        ActDeModel entity = new ActDeModel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
